home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / briksrc.zoo / turboc.c < prev    next >
C/C++ Source or Header  |  1989-08-04  |  9KB  |  277 lines

  1. /* nextfile.c */
  2. /* ::[[ @(#) turboc.c 1.7 89/07/12 03:18:30 ]]:: */
  3. #ifndef LINT
  4. static char sccsid[]="::[[ @(#) turboc.c 1.7 89/07/12 03:18:30 ]]::";
  5. #endif
  6.  
  7. /*
  8. This file is used only for MS-DOS.  It is used with Turbo C 1.0.  It also
  9. apparently works with Microsoft C 5.1 (see makefile.msc).
  10. */
  11.  
  12. /*
  13. Checksum:   60385500      (check or update this with "brik")
  14. */
  15.  
  16. /*
  17. nextfile() is a general wildcard expansion function that may be used
  18. with other programs.  Usage instructions are below.  It does not
  19. simply expand wildcards in an entire argument list.  Instead, it is
  20. called in a loop as described below, and returns one matching
  21. filename each time it is called.
  22.  
  23. These functions are for the SMALL MEMORY MODEL ONLY.
  24. */
  25.  
  26. #include "assert.h"
  27. #include "brik.h"
  28.  
  29. #define  FMAX  2        /* Number of different filename patterns */
  30. #define  PATHSIZE 200   /* Size of MS-DOS pathname */
  31. #define  NULL  0
  32.  
  33. #ifdef ANSIPROTO
  34. char *strtcpy (char *, char *);
  35. int strlen (char *);
  36. char *strcpy (char *, char *);
  37. #endif
  38.  
  39.  
  40. /* Structure definitions for MS-DOS software interrupt intdos() */
  41.  
  42. struct WORD_REGISTERS {
  43.    unsigned int ax, bx, cx, dx, si, di, carry, flags;
  44. };
  45.  
  46. /* byte registers */
  47.  
  48. struct BYTE_REGISTERS {
  49.    unsigned char al, ah, bl, bh, cl, ch, dl, dh;
  50. };
  51.  
  52. union REGS {
  53.    struct WORD_REGISTERS x;
  54.    struct BYTE_REGISTERS h;
  55. };
  56.  
  57. int intdos (union REGS *, union REGS *);
  58.  
  59. /*
  60. format of disk transfer address after MS-DOS calls FindFirst and
  61. FindNext
  62. */
  63. struct dta_t {
  64.    char junk[22];
  65.    int time;
  66.    int date;
  67.    long size;
  68.    char fname[13];
  69.    char just_in_case[4];   /* in case MS-DOS writes too much */
  70. };
  71.  
  72. void setdta (struct dta_t *);
  73. void fcbpath (struct dta_t *, char *, char *);
  74.  
  75. /*******************/
  76. /*
  77. nextfile() returns the name of the next source file matching a filespec.
  78.  
  79. INPUT
  80.    what: A flag specifying what to do.  If "what" is 0, nextfile()
  81.       initializes itself.  If "what" is 1, nextfile() returns the next
  82.       matching filename.
  83.    filespec:  The filespec, usually containing wildcard characters, that
  84.       specifies which files are needed.  If "what" is 0, filespec must be
  85.       the filespec for which matching filenames are needed.  If "what" is 1,
  86.       nextfile() does not use "filespec" and "filespec" should be NULL to
  87.       avoid an assertion error during debugging.
  88.    fileset:  nextfile() can keep track of more than one set of filespecs.
  89.       The fileset specifies which filespec is being matched and therefore
  90.       which set of files is being considered.  "fileset" can be in the
  91.       range 0:FMAX.  Initialization of one fileset does not affect the
  92.       other filesets.
  93.  
  94. OUTPUT
  95.    IF what == 0 THEN
  96.       return value is NULL
  97.    ELSE IF what == 1 THEN
  98.       IF a matching filename is found THEN
  99.          return value is pointer to matching filename including supplied path
  100.       ELSE
  101.          IF at least one file matched previously but no more match THEN
  102.             return value is NULL
  103.          ELSE IF supplied filespec never matched any filename THEN
  104.             IF this is the first call with what == 1 THEN
  105.                return value is pointer to original filespec
  106.             ELSE
  107.                return value is NULL
  108.             END IF
  109.          END IF
  110.       END IF
  111.    END IF
  112.  
  113. NOTE
  114.  
  115.    Initialization done when "what"=0 is not dependent on the correctness
  116.    of the supplied filespec but simply initializes internal variables
  117.    and makes a local copy of the supplied filespec.  If the supplied
  118.    filespec was illegal, the only effect is that the first time that
  119.    nextfile() is called with "what"=1, it will return the original
  120.    filespec instead of a matching filename.  That the filespec was
  121.    illegal will become obvious when the caller attempts to open the
  122.    returned filename for input/output and the open attempt fails.
  123.  
  124. USAGE HINTS
  125.  
  126. nextfile() can be used in the following manner:
  127.  
  128.       char *filespec;                  -- will point to filespec
  129.       char *this_file;                 -- will point to matching filename
  130.       filespec = parse_command_line(); -- may contain wildcards
  131.       FILE *stream;
  132.  
  133.       nextfile (0, filespec, 0);          -- initialize fileset 0
  134.       while ((this_file = nextfile(1, (char *) NULL, 0)) != NULL) {
  135.          stream = fopen (this_file, "whatever");
  136.          if (stream == NULL)
  137.             printf ("could not open %s\n", this_file);
  138.          else
  139.             perform_operations (stream);
  140.       }
  141. */
  142.  
  143. char *nextfile (what, filespec, fileset)
  144. int what;                        /* whether to initialize or match      */
  145. register char *filespec;         /* filespec to match if initializing   */
  146. register int fileset;            /* which set of files                  */
  147. {
  148.    static struct dta_t new_dta [FMAX+1];     /* our own private dta        */
  149.    static int first_time [FMAX+1];
  150.    static char pathholder [FMAX+1][PATHSIZE]; /* holds a pathname to return */
  151.    static char saved_fspec [FMAX+1][PATHSIZE];/* our own copy of filespec   */
  152.    union REGS regs;
  153.  
  154.    assert(fileset >= 0 && fileset <= FMAX);
  155.    if (what == 0) {
  156.       assert(filespec != NULL);
  157.       strcpy (saved_fspec[fileset], filespec);  /* save the filespec */
  158.       first_time[fileset] = 1;
  159.       return ((char *) NULL);
  160.    }
  161.  
  162.    setdta (&new_dta[fileset]);   /* set new dta -- our very own */
  163.    assert(what == 1);
  164.    assert(filespec == NULL);
  165.    assert(first_time[fileset] == 0 || first_time[fileset] == 1);
  166.  
  167.    if (first_time[fileset]) {             /* first time -- initialize etc. */
  168.       /* find first matching file */
  169.       regs.h.ah = 0x4e;                   /* FindFirst MS-DOS call    */
  170.       regs.x.dx = (unsigned int) saved_fspec[fileset]; /* filespec to match */
  171.       regs.x.cx = 0;                      /* search attributes       */
  172.       intdos (®s, ®s);
  173.    } else {
  174.       /* find next matching file */
  175.       regs.h.ah = 0x4f;                   /* FindNext MS-DOS call     */
  176.       intdos (®s, ®s);
  177.    }
  178.  
  179.    if (regs.x.carry != 0) {            /* if error status                  */
  180.       if (first_time[fileset]) {       /*   if file never matched then     */
  181.          first_time[fileset] = 0;
  182.          return (saved_fspec[fileset]);/*      return original filespec    */
  183.       } else {                         /*   else                           */
  184.          first_time[fileset] = 0;      /*                                  */
  185.          return ((char *) NULL);         /*      return (NULL) for no more   */
  186.       }
  187.    } else {                                        /* a file matched */
  188.       first_time[fileset] = 0;
  189.       /* add path info  */
  190.       fcbpath (&new_dta[fileset], saved_fspec[fileset], pathholder[fileset]);
  191.       return (pathholder[fileset]);                /* matching path  */
  192.    }
  193. } /* nextfile */
  194.  
  195. /*******************/
  196. /* This function sets the dta to a new dta */
  197. void setdta (dta)
  198. struct dta_t *dta;
  199. {
  200.    union REGS regs;
  201.    regs.h.ah = 0x1a;                /* SetDTA Call       */
  202.    regs.x.dx = (unsigned int) dta;  /* new DTA address   */
  203.    intdos (®s, ®s);
  204. }
  205.  
  206. /*******************/
  207. /*
  208. fcbpath() accepts a pointer to the Disk Transfer Area, a character
  209. pointer to a pathname that may contain wildcards, and a character
  210. pointer to a buffer.  It copies into the buffer the path prefix from
  211. the pathname and the filename prefix from the DTA so that it forms a
  212. complete path.
  213. */
  214.  
  215. void fcbpath (dta, old_path, new_path)
  216. struct dta_t *dta;
  217. char *old_path;
  218. register char *new_path;
  219. {
  220.    register int i;
  221.    int length, start_pos;
  222.  
  223.    strcpy(new_path, old_path);               /* copy the whole thing first */
  224.    length = strlen(new_path);
  225.    i = length - 1;                           /* i points to end of path */
  226.    while (i >= 0 && new_path[i] != '/' && new_path[i] != '\\' && new_path[i] != ':')
  227.       i--;
  228.    /* either we found a "/", "\", or ":", or we reached the beginning of
  229.       the name.  In any case, i points to the last character of the
  230.       path part. */
  231.    start_pos = i + 1;
  232.    for (i = 0; i < 13; i++)
  233.       new_path[start_pos+i] = dta->fname[i];
  234.    new_path[start_pos+13] = '\0';
  235. }
  236. /* -- END OF nextfile() and related functions -- */
  237.  
  238. extern unsigned _stklen = 30000;
  239.  
  240. #include <conio.h>
  241. void brktst() { kbhit(); }      /* test for